Блоги
namespace Blog;
class BlogModel{
var $errors = null;
var $fields = array();
var $user;
var $modx;
var $rewrite;
var $formatter;
public function __construct($user,$link,&$modx,&$rewrite){
$this->user = $user;
$this->fields['link'] = $link;
$this->modx = $modx;
$this->formatter = new \IntlDateFormatter('uk_UA', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::FULL, date_default_timezone_get());
$this->formatter->setPattern ('E, d MMM Y');
$this->rewrite = $rewrite;
}
public function tagLink($tag){
$tag = trim($tag);
$url = $this->rewrite->makeUrl('blog','tag',$tag);
return "
{$tag}";
}
public function tagFormatter($tags){
$taglist = explode(',',$tags);
$taglist = array_map(array($this,"tagLink"), $taglist);
return implode(', ',$taglist);
}
public function add(){
$ret = $this->addAction();
if($ret == false || (count($errors)>0)) {
$this->populateForm();
$this->addForm();
}else echo "Запис створено";
}
public function populateForm($id=null){
if(isset($id) &&($id>0)&& ($_SERVER['REQUEST_METHOD']!=='POST')){
$table_name = $this->modx->getFullTableName( 'binliz_blog' );
$table_tags_name = $this->modx->getFullTableName( 'binliz_blog_tags' );
$select = $this->modx->db->select('*',$table_name,"id={$id}");
if( $this->modx->db->getRecordCount( $select ) >= 1 ) {
if( $row = $this->modx->db->getRow( $select ) ) {
$this->fields = $row;
$this->fields['path'] = '/assets/templates/new/userfiles/';
$this->fields['active-select'] = $row['active']==1?'checked':'';
$selectTags = $this->modx->db->select('group_concat(tag) as tags',$table_tags_name,"blog_id={$id}");
if( $this->modx->db->getRecordCount( $selectTags ) >= 1 ) {
if( $tagsrow = $this->modx->db->getRow( $selectTags ) ) {
$this->fields['tags'] = $tagsrow['tags'];
}
}
}
}
}
}
public function addForm(){
$template = file_get_contents(dirname(__DIR__).'/templates/addFrom.php');
echo $this->modx->parseText($template,$this->fields);
}
public function addAction(){
if($_SERVER['REQUEST_METHOD']=='POST'){
$arFields = array();
$arFields['alias'] = $_POST['alias']?$this->modx->db->escape($_POST['alias']):null;
$arFields['name'] = $_POST['name']?$this->modx->db->escape($_POST['name']):null;
$arFields['preview_picture'] = $_POST['preview_picture']?$this->modx->db->escape($_POST['preview_picture']):null;
$arFields['preview_text'] = $_POST['preview_text']?$this->modx->db->escape($_POST['preview_text']):null;
$arFields['detail_picture'] = $_POST['detail_picture']?$this->modx->db->escape($_POST['detail_picture']):null;
$arFields['detail_text'] = $_POST['detail_text']?$this->modx->db->escape($_POST['detail_text']):null;
$arFields['active'] = $_POST['active']?1:0;
$arFields['user_id'] = $this->modx->getLoginUserID();
$arTags = explode(',',$_POST['tags']);
foreach($arTags as $key=>$item){
$item = trim((string)$item);
if(strlen($item)>=3) $arTags[$key] = strtolower($item);
else unset($arTags[$key]);
}
$arTags = array_unique($arTags);
if(strlen($arFields['name'])<3) return false;
if(strlen($arFields['alias'])<3) return false;
$table_name = $this->modx->getFullTableName( 'binliz_blog' );
$table_name_tags = $this->modx->getFullTableName( 'binliz_blog_tags' );
if(intval($_POST['id'])>0){
$id = intval($_POST['id']);
unset($arFields['user_id']);
$this->modx->db->update( $arFields, $table_name,"id={$id}");
$this->modx->db->delete($table_name_tags, "blog_id = {$id}");
} else{
$id = $this->modx->db->insert( $arFields, $table_name);
}
foreach($arTags as $item){
$item = strtolower($item);
$this->modx->db->insert( array('blog_id'=>$id,'tag'=>$item), $table_name_tags);
}
return $id;
}
else return false;
}
public function GetList($filter=null,$paginate=array('page'=>1,'count'=>10)){
$table_name = $this->modx->getFullTableName( 'binliz_blog' );
$table_user_name = $this->modx->getFullTableName( 'web_users' );
$table_tags_name = $this->modx->getFullTableName( 'binliz_blog_tags' );
$paginate['page']=($paginate['page']-1)*$paginate['count'];
$arfilter = array();
$jointable = "";
if($filter){
foreach($filter as $key=>$item){
if($key=='username') $key = 'user_names.username';
if($key=='tag') {
$key = 'tagslist.tag';
$jointable = "left join {$table_tags_name} as tagslist on tagslist.blog_id = blog_items.id ";
}
$arfilter[$key]= $this->modx->db->escape($item);
}
}
$fltlist = array();
foreach($arfilter as $key=>$item){
$fltlist[] = "{$key}= \"{$item}\"";
}
$fltlist = implode(' and ',$fltlist);
$table_user_atr_name = $this->modx->getFullTableName( 'web_user_attributes' );
$query = "SELECT blog_items.id,blog_items.name,blog_items.alias,blog_items.preview_picture,blog_items.date_pub,blog_items.date_update,blog_items.preview_text,blog_items.deleted,blog_items.active,blog_items.user_id, group_concat(tags.tag) as tags, user_names.username as user_alias from {$table_name} as blog_items ".
"left join {$table_tags_name} as tags on tags.blog_id = blog_items.id ".
"left join {$table_user_name} as user_names on user_names.id = blog_items.user_id ";
$query .= $jointable;
$query.="where {$fltlist} group by blog_items.id order by id desc, date_pub desc limit {$paginate['page']},{$paginate['count']}";
$select = $this->modx->db->query( $query);
$template = file_get_contents(dirname(__DIR__).'/templates/list.php');
if( $this->modx->db->getRecordCount( $select ) >= 1 ) {
while( $row = $this->modx->db->getRow( $select ) ) {
if($row['user_id'] == $this->modx->getLoginUserID()){
$row['edit-links'] = "
";
}
if($this->modx->isMemberOfWebGroup( array('Редактори сайту'))){
$row['edit-links'] = "
";
}
$row['link'] = $this->rewrite->makeUrl('blog','blogdetail',$row['user_alias'],$row['alias']);
$row['linkblog'] = $this->rewrite->makeUrl('blog','blog',$row['user_alias']);
if($row['date_pub'])
$row['pub_date'] = new \DateTime($row['date_pub']);
else
$row['pub_date'] = new \DateTime($row['date_update']);
$row['pub_date'] = $this->formatter->format($row['pub_date']);
$row['tags'] = $this->tagFormatter($row['tags']);
echo $this->modx->parseText($template,$row);
}
}
}
public function GetByAlias($alias){
}
public function GetUserIDbyAlias($useralias){
$this->getList(array('username'=>$useralias));
}
public function getUsersWrite(){
$table_name = $this->modx->getFullTableName( 'binliz_blog' );
$table_tags_name = $this->modx->getFullTableName( 'binliz_blog_tags' );
$table_user_name = $this->modx->getFullTableName( 'web_users' );
$table_user_atr_name = $this->modx->getFullTableName( 'web_user_attributes' );
$select = $this->modx->db->query(
"SELECT count( blog_items.id) as cnt, user_names.username as user_alias from {$table_name} as blog_items ".
"left join {$table_user_name} as user_names on user_names.id = blog_items.user_id ".
"where `active`=1 and `deleted`=0 group by user_id limit 0,30"
);
if( $this->modx->db->getRecordCount( $select ) >= 1 ) {
echo "
Блогери
";
echo "
";
while( $row = $this->modx->db->getRow( $select ) ) {
$url = $this->rewrite->makeUrl('blog','blog',$row['user_alias']);
echo "- {$row['cnt']}{$row['user_alias']}
";
}
echo "
";
}
}
public function getTags(){
$table_name = $this->modx->getFullTableName( 'binliz_blog' );
$table_tags_name = $this->modx->getFullTableName( 'binliz_blog_tags' );
$select = $this->modx->db->query("SELECT tag,count(tag) as cnt FROM {$table_tags_name} ".
" LEFT JOIN {$table_name} as blogpost ON blogpost.id = blog_id WHERE blogpost.deleted=0 and blogpost.active=1 group by tag order by cnt desc, tag asc limit 0,10");
if( $this->modx->db->getRecordCount( $select ) >= 1 ) {
echo "
Популярні теги
";
echo "
";
while( $row = $this->modx->db->getRow( $select ) ) {
$url = $this->rewrite->makeUrl('blog','tag',$row['tag']);
echo "- {$row['cnt']}{$row['tag']}
";
}
echo "
";
}
}
public function Edit($id){
$canEdit = false;
$table_name = $this->modx->getFullTableName( 'binliz_blog' );
$id = intval($this->modx->db->escape($id));
if($this->modx->isMemberOfWebGroup( array('Зареєстровані користувачі','Редактори сайту'))){
if($this->modx->isMemberOfWebGroup( array('Редактори сайту'))) $canEdit = true;
else{
$user_id = $this->modx->getLoginUserID();
$select = $this->modx->db->query(
"SELECT blog_items.id from {$table_name} as blog_items where user_id= {$user_id} and id={$id}"
);
if( $this->modx->db->getRecordCount( $select ) >= 1 ) $canEdit= true;
}
if($canEdit){
$ret = $this->addAction();
if($ret == false || (count($errors)>0)) {
$this->populateForm($id);
$this->addForm();
}else echo "Запис змінено";
}
}
}
public function GetDetail($filter=null){
$table_name = $this->modx->getFullTableName( 'binliz_blog' );
$table_user_name = $this->modx->getFullTableName( 'web_users' );
$table_tags_name = $this->modx->getFullTableName( 'binliz_blog_tags' );
$arfilter = array();
$jointable = "";
if($filter){
foreach($filter as $key=>$item){
$arfilter[$key]= $this->modx->db->escape($item);
}
}
$fltlist = array();
foreach($arfilter as $key=>$item){
$fltlist[] = "`{$key}`= \"{$item}\"";
}
$fltlist = implode(' and ',$fltlist);
$table_user_atr_name = $this->modx->getFullTableName( 'web_user_attributes' );
$query = "SELECT blog_items.*, group_concat(tags.tag) as tags, user_names.username as user_alias from {$table_name} as blog_items ".
"left join {$table_tags_name} as tags on tags.blog_id = blog_items.id ".
"left join {$table_user_name} as user_names on user_names.id = blog_items.user_id ";
$query .= $jointable;
$query.="where {$fltlist} AND blog_items.deleted=0 AND blog_items.active = 1";
$select = $this->modx->db->query( $query);
$template = file_get_contents(dirname(__DIR__).'/templates/detail.php');
if( $this->modx->db->getRecordCount( $select ) >= 1 ) {
while( $row = $this->modx->db->getRow( $select ) ) {
if($row['user_id'] == $this->modx->getLoginUserID()){
$row['edit-links'] = "
";
}
if($this->modx->isMemberOfWebGroup( array('Редактори сайту'))){
$row['edit-links'] = "
";
}
$row['link'] = $this->rewrite->makeUrl('blog','blogdetail',$row['user_alias'],$row['alias']);
$row['linkblog'] = $this->rewrite->makeUrl('blog','blog',$row['user_alias']);
if($row['date_pub'])
$row['pub_date'] = new \DateTime($row['date_pub']);
else
$row['pub_date'] = new \DateTime($row['date_update']);
$row['pub_date'] = $this->formatter->format($row['pub_date']);
$row['tags'] = $this->tagFormatter($row['tags']);
echo $this->modx->parseText($template,$row);
}
}
}
}Тимчасово недоступно...